<template>
  <div class="style-page">
    <div class="head-page-title">{{ showComponentData.title }}</div>
    <div class="line"></div>
    <div class="head-title">组件样式</div>

    <div class="component-style">
      <el-form :model="form">
        <el-form-item label="标题布局：">
          <el-radio-group v-model="form.itemStyle.textAlign">
            <el-radio value="left">居左</el-radio>
            <el-radio value="center">居中</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="标题名称：">
          <el-input v-model="showComponentData.title" />
        </el-form-item>
        <el-form-item label="文字大小：">
          <div class="flex">
            <el-slider style="width: 70%" v-model="form.itemStyle.fontSize" :min="12" :max="30" />
            <el-input
              type="number"
              style="width: 30%; margin-left: 20px"
              v-model.number="form.itemStyle.fontSize"
              :min="12"
              :max="30"
            >
              <template #append>px</template>
            </el-input>
          </div>
        </el-form-item>
        <el-form-item label="字体颜色：">
          <el-color-picker v-model="form.itemStyle.color"></el-color-picker>
          <el-input style="width: 160px; margin-left: 10px" v-model="form.itemStyle.color">
            <template #append>
              <el-button @click="form.itemStyle.color = '#333333'">重置</el-button>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item label="字体粗细：">
          <el-radio-group v-model="form.itemStyle.fontWeight">
            <el-radio label="正常" :value="0" />
            <el-radio label="加粗" :value="600" />
          </el-radio-group>
        </el-form-item>
        <el-form-item label="查看更多：">
          <el-radio-group v-model="showComponentData.show_more">
            <el-radio :value="true">显示</el-radio>
            <el-radio :value="false">隐藏</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="字体名称：" v-if="showComponentData.show_more">
          <el-input v-model="showComponentData.more_text"></el-input>
        </el-form-item>
        <el-form-item label="字体颜色：">
          <el-color-picker v-model="form.itemStyle.more_color"></el-color-picker>
          <el-input style="width: 160px; margin-left: 10px" v-model="form.itemStyle.more_color">
            <template #append>
              <el-button @click="form.itemStyle.more_color = '#333333'">重置</el-button>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item label="跳转链接：" v-if="showComponentData.show_more">
          <el-link type="primary" @click="showLinkDataDialog = true">{{
            showComponentData.link?.name || '请选择跳转链接'
          }}</el-link>
        </el-form-item>
      </el-form>
    </div>

    <div class="component-style">
      <common-style
        :content-data="showComponentData"
        @update-style="handleUpdateStyle"
      ></common-style>
    </div>

    <video-group-dialog
      v-if="showVideoListDialog"
      :show-video-dialog="showVideoListDialog"
      @close-dialog="handleCloseDialog"
    ></video-group-dialog>

    <link-list-dialog
      v-if="showLinkDataDialog"
      :show-link-dialog="showLinkDataDialog"
      @close-dialog="handleCloseLinkDialog"
    ></link-list-dialog>
  </div>
</template>

<script setup lang="ts">
  import CommonStyle from '@/views/decoration/diyComponents/CommonStyleComponent/CommonStyle.vue'
  import VideoGroupDialog from '@/views/decoration/diyComponents/VideoGroupDialog.vue'
  import debounce from 'lodash/debounce'
  import LinkListDialog from '@/views/decoration/diyComponents/LinkListDialog.vue'

  const props = defineProps({
    contentData: {
      type: Object,
      default() {
        return {}
      }
    }
  })

  const form = reactive({
    itemStyle: {
      fontSize: 18,
      color: '#000000',
      fontWeight: 600,
      height: 100,
      textAlign: 'left',
      more_color: '#787878'
    }
  })

  const showLinkDataDialog = ref(false)

  watch(
    form,
    () => {
      emitFormUpdate()
    },
    { deep: true }
  )

  const emitFormUpdate = debounce(() => {
    const raw = toRaw(form)
    showComponentData.value.itemStyle = raw.itemStyle
    handleUpdateComponent()
  }, 300)

  const emit = defineEmits(['updateData'])

  const showComponentData = ref(props.contentData)
  const showVideoListDialog = ref(false)

  function handleUpdateComponent() {
    emit('updateData', showComponentData.value)
  }

  function handleUpdateStyle(val: any) {
    showComponentData.value.style = val
    handleUpdateComponent()
  }

  function handleCloseLinkDialog(value: any) {
    if (value) {
      showComponentData.value.link = value
    }
    showLinkDataDialog.value = false
  }

  function handleCloseDialog(value: any) {
    if (value) {
      showComponentData.value.default_sub_list.push(...value)
    }
    showVideoListDialog.value = false
  }

  onMounted(() => {
    if (showComponentData.value.itemStyle) {
      form.itemStyle.fontSize = showComponentData.value.itemStyle.fontSize
      form.itemStyle.color = showComponentData.value.itemStyle.color
      form.itemStyle.fontWeight = showComponentData.value.itemStyle.fontWeight
      form.itemStyle.height = showComponentData.value.itemStyle.height
      form.itemStyle.textAlign = showComponentData.value.itemStyle.textAlign
      form.itemStyle.more_color = showComponentData.value.itemStyle.more_color
    }
  })
</script>

<style scoped lang="scss">
  .style-page {
    padding: 20px;

    .head-page-title {
      padding-top: 10px;
      padding-bottom: 20px;
      font-size: 18px;
      font-weight: bold;
      color: black;
    }

    .head-title {
      padding: 20px 0;
      font-size: 14px;
      font-weight: bold;
      color: black;
    }

    .line {
      width: 100%;
      height: 1px;
      background: #f5f7fa;
    }

    .add-area {
      width: 100%;

      .add-title {
        font-size: 14px;
        color: black;
        font-weight: bold;
        margin-bottom: 20px;
      }

      .content {
        .carousel-item {
          width: 100%;
          margin-bottom: 20px;
          position: relative;
          display: flex;
          flex-direction: column;
          background-color: #f7f8fa;
          padding: 10px;
          box-sizing: border-box;

          &:hover {
            .del-icon {
              display: block;
            }
          }

          .video-title {
            margin-bottom: 20px;
          }

          .video-content {
            width: 100%;
            position: relative;

            .video-thumb {
              width: 100%;
              height: 200px;
              background-color: #fff;
            }

            .mask {
              position: absolute;
              bottom: 0;
              left: 0;
              width: 100%;
              height: 30px;
              background-color: rgba(0, 0, 0, 0.4);
              display: flex;
              align-items: center;
              justify-content: center;
              opacity: 0;
              transition: opacity 0.3s ease;

              .opera {
                .upload-text {
                  color: #fff;
                  font-size: 14px;
                  margin-right: 10px;
                  cursor: pointer;
                }
              }
            }

            &:hover .mask {
              opacity: 1;
            }
          }

          .link-area {
            width: 100%;
            display: flex;
            align-items: center;
            justify-content: space-between;
            margin-top: 20px;
            font-size: 14px;
          }

          .link {
            width: 80%;
            display: flex;
            align-items: center;
            height: 40px;
            padding-left: 10px;
            background-color: #fff;
          }

          .del-icon {
            display: none;
            position: absolute;
            top: -2px;
            right: -2px;
            cursor: pointer;
            color: #8c939d;
          }
        }
      }
    }

    .component-style {
      width: 100%;
      box-sizing: border-box;
      margin-top: 20px;

      .header {
        display: flex;
        justify-content: space-between;
        align-items: center;
        margin-bottom: 10px;

        .title {
          font-size: 16px;
          font-weight: bold;
        }
      }

      .flex {
        display: flex;
        justify-content: space-between;
        width: 100%;
        padding: 0 3px;
      }
    }
  }
</style>
